package com.pactera.madp.cp.service.sync.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.ftp.FtpConfig;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.cp.api.entity.sync.SyncFundStdDataInfoEntity;
import com.pactera.madp.cp.common.constant.Constants;
import com.pactera.madp.cp.common.util.sync.SFTPUtils;
import com.pactera.madp.cp.common.util.sync.SyncUtils;
import com.pactera.madp.cp.config.FundSftpConfig;
import com.pactera.madp.cp.config.SyncConfig;
import com.pactera.madp.cp.context.SyncStateContext;
import com.pactera.madp.cp.mapper.sync.SyncFundStdDataInfoMapper;
import com.pactera.madp.cp.service.sync.SyncFundStdDataInfoHisService;
import com.pactera.madp.cp.service.sync.SyncFundStdDataInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class SyncFundStdDataInfoServiceImpl extends ServiceImpl<SyncFundStdDataInfoMapper, SyncFundStdDataInfoEntity> implements SyncFundStdDataInfoService {

    private final SyncFundStdDataInfoHisService syncFundStdDataInfoHisService;
    private final SyncConfig syncConfig;
    private final FundSftpConfig fundSftpConfig;

    @Override
    public void addYesterdayToHis() {
        log.debug("基金行情信息：addYesterdayToHis");
        String date = DateUtil.format(new Date(), Constants.DATE_FORMAT);
        syncFundStdDataInfoHisService.syncOneDay(date);
    }

    @Override
    public void deleteYesterday() {
        log.debug("基金行情信息：deleteYesterday");
        this.remove(Wrappers.emptyWrapper());
    }

    @Override
    public void parse() {
        log.debug("FTP获取数据文件");
        String fileDate = SyncStateContext.get().getFileDate();
        // 下载文件设置
        String srcFtpFile = SyncUtils.buildFilePath(syncConfig.getFundFtpFile(), Constants.FN_FUND_STD_DATA, fileDate);
        String destFile = SyncUtils.buildFilePath(syncConfig.getFundPath(), Constants.FN_FUND_STD_DATA, fileDate);
        // 下载目录检测
        String destDir = SyncUtils.buildFilePath(syncConfig.getFundPath(), "", fileDate);
        boolean flag = SyncUtils.checkDir(destDir, true);
        if (!flag) {
            throw new RuntimeException("基金行情方案文件下载目录不存在");
        }
        // 文件下载
        FtpConfig ftpConfig = new FtpConfig();
        BeanUtil.copyProperties(fundSftpConfig, ftpConfig);
        flag = SFTPUtils.download(ftpConfig, srcFtpFile, destFile);
        if (!flag) {
            throw new RuntimeException("基金行情文件下载失败");
        }

        log.debug("基金行情信息：parse");
        File newestFile = SyncUtils.getNewestFile(destFile);
        if (ObjectUtil.isNull(newestFile)) {
            throw new RuntimeException("基金行情信息文件不存在");
        }
        List<SyncFundStdDataInfoEntity> entityList
                = SyncUtils.parse(newestFile, Constants.SEPARATOR_VERTICAL_LINE, SyncFundStdDataInfoEntity.class, syncConfig.getFundCharset());

        specialHandle(entityList);

        this.saveBatch(entityList);
    }

    @Override
    public void deleteHisOneDay() {
        log.debug("基金行情信息：deleteHisOneDay");
        DateTime offsetDay = DateUtil.offsetDay(new Date(), -30);
        String date = DateUtil.format(offsetDay, Constants.DATE_FORMAT);
        syncFundStdDataInfoHisService.deleteOneDate(date);
    }

    @Override
    public void syncBusiness() {
        log.debug("基金行情信息：syncBusiness");
        SyncStateContext.get().setFinalState(true);
    }

    // 行情信息主键设置
    private void specialHandle(List<SyncFundStdDataInfoEntity> entityList) {
        // 初始化主键值为1
        int id = 1;
        // 遍历为每个行情信息对象设置主键
        for (SyncFundStdDataInfoEntity e : entityList) {
            e.setId(id++);
        }
    }

}